package com.sany.taskmanager.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.sany.taskmanager.R;
import com.sany.taskmanager.databinding.ItemTaskInfoBinding;
import com.sany.taskmanager.room.entity.LocalTaskInfo;

import java.util.List;

/**
 * 待任务下发列表
 */
public class TaskInfoAdapter extends RecyclerView.Adapter<TaskInfoAdapter.VH> {

    private final Context mContext;
    private List<LocalTaskInfo> mTaskList;
    private OnOperateListener mOnOperateListener;
    private final boolean mCarModel;// 是否为车型号

    public void setTaskList(List<LocalTaskInfo> list) {
        mTaskList = list;
        notifyDataSetChanged();
    }

    public void setOnOperateListener(OnOperateListener onOperateListener) {
        mOnOperateListener = onOperateListener;
    }

    public TaskInfoAdapter(Context context) {
        this(context, false);
    }

    public TaskInfoAdapter(Context context, boolean carModel) {
        mContext = context;
        mCarModel = carModel;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemTaskInfoBinding itemTaskInfoBinding = ItemTaskInfoBinding.inflate(LayoutInflater.from(mContext), parent, false);
        return new VH(itemTaskInfoBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        LocalTaskInfo item = mTaskList.get(position);
        if (position % 2 == 1) {
            holder.itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_17325D));
        } else {
            holder.itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
        }

        if (mCarModel) {
            holder.tvDelete.setVisibility(View.GONE);
            holder.tvMaterialName.setVisibility(View.VISIBLE);
            holder.tvMaterialName.setText(item.getMaterialName());
            holder.tvCarModelNum.setVisibility(View.VISIBLE);
            holder.tvCarModelNum.setText(item.getCarModelNum());
        } else {
            holder.tvDelete.setVisibility(View.VISIBLE);
            holder.tvDelete.setOnClickListener((v) -> {
                if (mOnOperateListener != null) {
                    mOnOperateListener.delete(item);
                }
            });
            holder.tvMaterialName.setVisibility(View.GONE);
            holder.tvCarModelNum.setVisibility(View.GONE);
        }

        holder.tvNum.setText(String.valueOf(position + 1));
        holder.tvMaterialNum.setText(item.getMaterialNum());
        holder.tvBoomHeadColor.setText(item.getHeadColorCode());
        holder.tvBoomColor.setText(item.getColorCode());
        holder.tvCount.setText(String.valueOf(item.getCount()));
        holder.ivSelect.setImageLevel(item.isSelected() ? 1 : 0);
        holder.tvNewBoom.setText(item.getBoomType() == 1 ? mContext.getString(R.string.label_yes) : mContext.getString(R.string.label_no));

//        holder.tvSend.setOnClickListener((v) -> {
//            if (mOnOperateListener != null) {
//                mOnOperateListener.send(item);
//            }
//        });
        holder.tvModify.setOnClickListener((v) -> {
            if (mOnOperateListener != null) {
                mOnOperateListener.modify(holder.getAdapterPosition());
            }
        });
        holder.ivSelect.setOnClickListener((v) -> {
            item.setSelected(!item.isSelected());
            notifyItemChanged(position);
        });
    }

    @Override
    public int getItemCount() {
        return mTaskList == null ? 0 : mTaskList.size();
    }

    static class VH extends RecyclerView.ViewHolder {

        public ImageView ivSelect;
        public TextView tvNum;
        public TextView tvMaterialNum;
        public TextView tvBoomHeadColor;
        public TextView tvBoomColor;
        public TextView tvCount;
        public TextView tvNewBoom;
        public TextView tvSend;
        public TextView tvModify;
        public TextView tvDelete;
        public TextView tvMaterialName;
        public TextView tvCarModelNum;

        public VH(@NonNull ItemTaskInfoBinding itemTaskInfoBinding) {
            super(itemTaskInfoBinding.getRoot());
            ivSelect = itemTaskInfoBinding.ivSelect;
            tvNum = itemTaskInfoBinding.tvNum;
            tvMaterialNum = itemTaskInfoBinding.tvMaterialNum;
            tvBoomHeadColor = itemTaskInfoBinding.tvHeadColor;
            tvBoomColor = itemTaskInfoBinding.tvColor;
            tvCount = itemTaskInfoBinding.tvCount;
            tvSend = itemTaskInfoBinding.tvSend;
            tvModify = itemTaskInfoBinding.tvModify;
            tvDelete = itemTaskInfoBinding.tvDelete;
            tvNewBoom = itemTaskInfoBinding.tvNewBoom;
            tvMaterialName = itemTaskInfoBinding.tvMaterialName;
            tvCarModelNum = itemTaskInfoBinding.tvCarModel;
        }
    }

    public interface OnOperateListener {
//        void send(LocalTaskInfo item);

        void modify(int position);

        void delete(LocalTaskInfo item);
    }

}
